package com.tudouji.project.organization.controller;

import java.util.List;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 组织管理Controller
 *
 * @author tudouji
 * @date 2020-12-28
 */
@RestController
@RequestMapping("/organization/organization")
public class OrganizationController extends BaseController {
    @Autowired
    private IOrganizationService organizationService;

    @GetMapping("/getlist")
    public AjaxResult getList(Organization organization) {
        organization.setStatus(1);
        List<Organization> list = organizationService.selectOrganizationList(organization);
        return AjaxResult.success(list);
    }

    /**
     * 查询组织管理列表
     */
//    @PreAuthorize("@ss.hasPermi('organization:organization:list')")
    @GetMapping("/list")
    public TableDataInfo list(Organization organization) {
        startPage();
        List<Organization> list = organizationService.selectOrganizationList(organization);
        return getDataTable(list);
    }

    /**
     * 导出组织管理列表
     */
    @PreAuthorize("@ss.hasPermi('organization:organization:export')")
    @Log(title = "组织管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Organization organization) {
        List<Organization> list = organizationService.selectOrganizationList(organization);
        ExcelUtil<Organization> util = new ExcelUtil<Organization>(Organization.class);
        return util.exportExcel(list, "organization");
    }

    /**
     * 获取组织管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('organization:organization:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(organizationService.selectOrganizationById(guid));
    }

    /**
     * 新增组织管理
     */
    @PreAuthorize("@ss.hasPermi('organization:organization:add')")
    @Log(title = "组织管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Organization organization) {
        organization.setGuid(IdUtils.fastSimpleUUID());
        organization.setCreatetime(DateUtils.getNowDate());
        organization.setOperatorid(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        return toAjax(organizationService.insertOrganization(organization));
    }

    /**
     * 修改组织管理
     */
    @PreAuthorize("@ss.hasPermi('organization:organization:edit')")
    @Log(title = "组织管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Organization organization) {
        return toAjax(organizationService.updateOrganization(organization));
    }

    /**
     * 删除组织管理
     */
    @PreAuthorize("@ss.hasPermi('organization:organization:remove')")
    @Log(title = "组织管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(organizationService.deleteOrganizationByIds(guids));
    }

    @GetMapping("/findOrganizationList")
    public AjaxResult findOrganizationList(Organization organization) {
        List<Organization> list = organizationService.selectOrganizationList(organization);
        return AjaxResult.success(list);
    }
}
